package com.example.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @author ChenXiaoBin
 * on 2019/5/16
 */
@RestController
@RequestMapping("/V1.0/manager/pool")
public class TreadPoolExController {
    private static ExecutorService fixedThreadPool =
            new ThreadPoolExecutor(1, 1,
                    0L, TimeUnit.MILLISECONDS,
                    new LinkedBlockingQueue<>());

    public void run() {
        fixedThreadPool.execute(() -> {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public  static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads, 0L,
                TimeUnit.MICROSECONDS,
                new LinkedBlockingDeque<Runnable>());
    }

    @PutMapping("")
    @ApiOperation(value = "测试thread工具类", notes = "测试thread工具类")
    public void thread() {
        Boolean result = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int count = 0;
        while (!result) {
            try {
                Thread.sleep(5000);
                count++;
                System.out.println(sdf.format(new Date()) + "--循环执行第" + count + "次");
                if (count == 5) {
                    result = true;
                    break ;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



}
